package com.thermal.thermal_inspection.util;

import com.alibaba.fastjson.JSON;
import com.thermal.thermal_inspection.model.R;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpResponse;
import reactor.core.publisher.Mono;
public class ResponseUtil {

    public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response,
                                                   String contentType,
                                                   HttpStatus status,
                                                   Object message,
                                                   int code) {
        response.setStatusCode(status);
        response.getHeaders().add(HttpHeaders.CONTENT_TYPE, contentType);
        R<?> result = R.fail(code, message.toString());
        byte[] bytes = JSON.toJSONString(result).getBytes();
        DataBuffer buffer = response.bufferFactory().wrap(bytes);
        return response.writeWith(Mono.just(buffer));
    }
}

